<?php
set_include_path("../include;include");
require_once "/util/helperFunctions.php";
require_once "dao/EventDAO.php";
require_once "model/Supplement.php";
require_once 'model/PriceCategory.php';
require_once 'model/Registration.php';


class Event {
	private $id;
	private $subEvent;
	private $name;
	private $description;
	private $code;
	private $only_members;
	private $has_autocomplete;
	private $prefix;
	private $datetime;
	//TODO een veld met de datum waarop het doorgaat, ook in DAO

	function __construct() {

	}

	/**
	 * Static constructor function with fields as arguments
	 * @param string $name The name of the event
	 * @param string $description The description of the vent
	 * @param string $code The code of the event (passcode), can be null
	 * @param boolean $only_members Whether the possible participants are only from the member list or not
	 * @param boolean $has_autocomplete Whether autocomplete should be enabled or not
	 */
	public static function createNew(string $name, string $description, string $code, boolean $only_members, boolean $has_autocomplete, string $prefix) {
		$instance = new self();
		$instance->subEvent = null;
		$instance->name = $name;
		$instance->description = $description;
		$instance->code = $code;
		$instance->only_members = $only_members;
		$instance->has_autocomplete = $has_autocomplete;
		$instance->prefix = $prefix;
		return $instance;
	}

	/**
	 * Save this event.
	 * @return The id of the saved event
	 */
	public function save() {
		$dao = EventDAO::getInstance();

		$this->datetime = time();
		$myid = $dao->save($this);
		$this->id = $myid;
		return $myid;
	}

	/**
	 * Loads the fields of this event by looking for the values corresponding to the given id.
	 * @param $id The id of which the corresponding fields should be loaded in this event
	 */
	public function load(integer $id) {
		$dao = EventDAO::getInstance();

		$this->id = $id;
		$this->subEvent = Event::loadEvent($dao->getSubEventIdByEventId($id));
		$this->name = $dao->getNameByEventId($id);
		$this->description = $dao->getDescriptionByEventId($id);
		$this->code = $dao->getCodeByEventId($id);
		$this->only_members = $dao->getOnlyMembersByEventId($id);
		$this->has_autocomplete = $dao->getHasAutoCompleteByEventId($id);
		$this->prefix = $dao->getPrefixByEventId($id);
		$this->datetime = $dao->getDatetimeByEventId($id);
	}

	/**
	 * Static save method, to save an Event object from somwhere else;
	 * @param Event $event The Event that is to be saved
	 */
	public static function saveEvent(Event $event) {
		$dao = EventDAO::getInstance();

		$event->setDatetime(time());
		$event_id = $dao->save($event);
		$event->setId($event_id);
	}

	/**
	 * Static load method, to load an existing Event object from somewhere else, using its id.
	 * @param integer $id The unique id of the Event
	 */
	public static function loadEvent(integer $id) {
		if (is_null($id) || $id < 0) {
			return null;
		} else {
			$event = new self();
			$event->id = $id;
			$event->load($id);
			return $event;
		}
	}

	/**
	 * Returns all events ordered by id.
	 * @return An array of Events ordered by id.
	 */
	public static function getAllEvents() {
		$dao = EventDAO::getInstance();
		$events = $dao->getAllEvents(); 
		return (array) $events;
	}
	
	/**
	 * Returns all events ordered by save-date.
	 * @param $asc A boolean value, true if ASCENDING order, false if DESCENDING order.
	 * @return An array of Events ordered by date
	 */
	public static function getAllEventsOrderedByDate($asc) {
		$dao = EventDAO::getInstance();
		$events = $dao->getAllEventsOrderedByDate($asc); 
		return (array) $events;
	}

	/**
	 * Get all registrations of a this Event.
	 * @return An array of Registration objects
	 */
	public function getAllRegistrations() {
		return (array) Registration::getAllRegistrations($this);
	}

	/**
	 * Creates a new Registration object for this event.
	 * This object has not been saved to the database yet! Call ->save() on it to do that.
	 * @return A new Registration object for this event.
	 */
	public function createRegistration() {
		$registration = new Registration($this);
		$registration->setPayed(false);
		return $registration;
	}

	/**
	 * Get an array of all associated PriceCategory objects.
	 * @return An array of PriceCategory objects
	 */
	public function getPriceCategories() {
		$cats = PriceCategory::getPriceCategoriesFor($this);
		return $cats;
	}

	public function getSupplements() {
		$supps = Supplement::allSupplementsOf($this);
		return $supps;
	}

	public function hasSubEvent() {
		return !is_null($this->subEvent);
	}


	/* Getters and setters */

	public function getId() {
		return $this->id;
	}

	public function getSubEvent() {
		return $this->subEvent;
	}

	public function getName() {
		return $this->name;
	}

	public function getDescription() {
		return $this->description;
	}

	public function getCode() {
		return $this->code;
	}

	public function getOnly_members() {
		return $this->only_members;
	}

	public function getHas_autocomplete() {
		return $this->has_autocomplete;
	}
	
	/**
	 * This is the date-time but in timestamp integer format..
	 */
	public function getDatetime() {
		return $this->datetime;
	}
	
	public function setId(integer $id) {
		$this->id = $id;
	}

	public function setSubEvent($subEvent) {
		$this->subEvent = $subEvent;
	}

	public function setName(string $name) {
		$this->name = $name;
	}

	public function setDescription(string $description) {
		$this->description = $description;
	}

	public function setCode(string $code) {
		$this->code = $code;
	}

	public function setOnly_members(boolean $only_members) {
		$this->only_members = $only_members;
	}

	public function setHas_autocomplete(boolean $has_autocomplete) {
		$this->has_autocomplete = $has_autocomplete;
	}
	
	public function setDatetime(integer $datetime) {
		$this->datetime = $datetime;
	}

	public function getPrefix() {
		return $this->prefix;
	}

	public function setPrefix(string $prefix) {
		$this->prefix = $prefix;
	}

	public function __toString() {
		return "Event: ".$this->name;
	}

}